package lex.title.basic;

import java.awt.*;
import javax.swing.JRootPane;

/* 
 * maybe we could make a fvwm-like layout here,
 * it's more generic and flexible 
 */
public abstract class BasicTitleLayout implements LayoutManager {
	private static final int DEFAULT_TITLE_SPACE = 7;
	private static final int DEFAULT_BUTTON_GAP = 7;
	private static final int DEFAULT_EDGE_GAP = 9;
	private BasicWindowTitle title = null;
	private int titleExtraSpace = DEFAULT_TITLE_SPACE;
	private int buttonGap = DEFAULT_BUTTON_GAP;
	private int edgeGap = DEFAULT_EDGE_GAP;

	public BasicTitleLayout(BasicWindowTitle title) {
		this.title = title;
	}

	public void addLayoutComponent(String name, Component c) {
	}

	public void removeLayoutComponent(Component c) {
	}

	public Dimension minimumLayoutSize(Container c) {
		return preferredLayoutSize(c);
	}

	public Dimension preferredLayoutSize(Container c) {
		JRootPane rootPane = title.getRootPane();
		FontMetrics fm = rootPane.getFontMetrics(rootPane.getFont());
		int fontHeight = fm.getHeight();
		fontHeight += titleExtraSpace;
		int iconHeight = 0;
		Image image = title.getIconImage();
		if (null != image)
			iconHeight = image.getHeight(title);
		int finalHeight = Math.max(fontHeight, iconHeight);
		return new Dimension(finalHeight, finalHeight);
	}

	protected void setTitleExtraSpace(int space) {
		if (space < 0)
			space = 0;
		this.titleExtraSpace = space;
	}

	protected int getTitleExtraSpace() {
		return this.titleExtraSpace;
	}

	protected int getButtonGap() {
		return buttonGap;
	}

	protected void setButtonGap(int buttonGap) {
		if (buttonGap < 0)
			buttonGap = 0;
		this.buttonGap = buttonGap;
	}

	protected int getEdgeGap() {
		return edgeGap;
	}

	protected void setEdgeGap(int edgeGap) {
		if (edgeGap < 0)
			edgeGap = 0;
		this.edgeGap = edgeGap;
	}

	/**
	 * let e --> egdeGap; b --> buttonGap<br>
	 * layout is:<br>
	 * [e](close)[b](max-restore)[b](min)-----...-----(menu)[e]<br>
	 * <br>
	 * we use WindowButton.getPreferredSize() to rebound the button<br>
	 */
	protected void layoutRightToLeft(Container c) {
		int w = title.getWidth();
		int h = title.getHeight();
		Dimension buttonSize = null;
		// layout close button
		buttonSize = title.getCloseButton().getPreferredSize();
		int x = edgeGap;
		int y = (h - buttonSize.height) / 2;
		title.getCloseButton().setLocation(x, y);
		// layout max-restore button
		buttonSize = title.getMaximizeRestoreButton().getPreferredSize();
		x += buttonSize.width + buttonGap;
		y = (h - buttonSize.height) / 2;
		title.getMaximizeRestoreButton().setLocation(x, y);
		// layout minimize button
		buttonSize = title.getMinimizeButton().getPreferredSize();
		x += buttonSize.width + buttonGap;
		y = (h - buttonSize.height) / 2;
		title.getMinimizeButton().setLocation(x, y);
		// layout menu button
		buttonSize = title.getMenuButton().getPreferredSize();
		x = w - edgeGap - buttonSize.width;
		y = (h - buttonSize.height) / 2;
		title.getMenuButton().setLocation(x, y);
	}

	/**
	 * <br>
	 * let e --> egdeGao; b --> buttonGap<br>
	 * layout is:<br>
	 * [e](menu)-----...-----(min)[b](max-restore)[b](close)[e]<br>
	 * <br>
	 * we use WindowButton.getPreferredSize() to rebound the button<br>
	 */
	protected void layoutLeftToRight(Container c) {
		int w = title.getWidth();
		int h = title.getHeight();
		Dimension buttonSize = null;
		// layout menu button
		buttonSize = title.getMenuButton().getPreferredSize();
		title.getMenuButton().setSize(buttonSize);
		int x = edgeGap;
		int y = (h - buttonSize.height) / 2;
		title.getMenuButton().setLocation(edgeGap, y);
		// layout close button
		buttonSize = title.getCloseButton().getPreferredSize();
		x = w - edgeGap - buttonSize.width;
		y = (h - buttonSize.height) / 2;
		title.getCloseButton().setLocation(x, y);
		// layout max-restore button
		buttonSize = title.getMaximizeRestoreButton().getPreferredSize();
		x -= buttonSize.width + buttonGap;
		y = (h - buttonSize.height) / 2;
		title.getMaximizeRestoreButton().setLocation(x, y);
		// layout minimize button
		buttonSize = title.getMinimizeButton().getPreferredSize();
		x -= buttonSize.width + buttonGap;
		y = (h - buttonSize.height) / 2;
		title.getMinimizeButton().setLocation(x, y);
	}
}
